const { get, post } = require('../../../utils/request.js')
const moment = require('../../../utils/moment.js')
const EventBus = require('../../../utils/eventBus.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tipActiveAccount: false,
    showPassword: false,
    childName: '',
    incomeMoney: '',
    childMoney: 0,
    bankAccountId: 0,
    operator: '',
    nowDate: '',
    remark: '',
    tags: [],
    saveWays: [],
    fixTimePenalty: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    get('family/info', null, ({ family: { memberAlias }, tags, yearRate, tipActiveAccount }) => {
      yearRate[0].active = true;
      this.setData({
        operator: memberAlias,
        nowDate: moment().format('YYYY年MM月DD日'),
        tags: tags,
        saveWays: yearRate,
        tipActiveAccount: tipActiveAccount
      })
    })

    get('familyconfig/list', {
      "filter_familyUnicode_eq_s": wx.getStorageSync('unicode'),
      "filter_name_eq_s": 'fixTimePenalty'
    }, (data) => {
      if (data && data.length > 0) {
        this.setData({
          fixTimePenalty: data[0].value
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  bindButtonTap() {

  },
  onChangeHead({ detail: { activeCard:
    { bankAccount: { currentBalance, id }, memberName } } }) {
    this.data.tags.forEach(item => [
      item.active = false
    ])
    this.setData({
      childName: memberName,
      childMoney: currentBalance,
      bankAccountId: id,
      incomeMoney: '',
      remark: '',
      tags: this.data.tags
    })
    this.onInputMoney({ detail: { value: 0 } })
  },
  onClickTag({ currentTarget: { id } }) {
    const tag = this.data.tags[id];
    tag.active = !tag.active;
    this.setData({
      tags: this.data.tags
    })
  },
  onSelectWay({ currentTarget: { id } }) {
    this.data.saveWays.forEach((item, index) => {
      if (index == id) {
        item.active = true
      } else {
        item.active = false
      }
    });
    this.setData({
      saveWays: this.data.saveWays
    })
  },
  onInputMoney({ detail: { value } }) {
    get('bankstatement/calculateInterest', {
      money: value,
      unicode: wx.getStorageSync('unicode')
    }, (yearRate) => {
      yearRate[0].active = true;
      this.setData({
        saveWays: yearRate
      })
    })
  },
  onSubmit() {
    const tags = this.data.tags
      .filter(p => p.active)
      .map(item => item.name)
      .join(',');
    const choosedWay = this.data.saveWays.filter(item => item.active)[0];
    const params = {
      incomeMoney: this.data.incomeMoney,
      bankAccountId: this.data.bankAccountId,
      currentBalance: this.data.childMoney,
      tags: tags,
      remark: this.data.remark,
      yearRate: choosedWay.rate,
      year: choosedWay.year,
      totalInterest: choosedWay.interest,
      operator: this.data.operator,
      type: 3
    }

    post('bankstatement/fixIncome', params, () => {
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000,
        success: function () {
          EventBus.emit('home.freshAllData')
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/tabBar/summary/home',
            })
          }, 2000);
        }
      })
    });

  },

  submitData() {

    if (this.data.tipActiveAccount) {
      wx.showToast({
        title: '请激活微行卡',
        icon: 'error',
        duration: 2000
      })
      return
    }

    if (this.data.bankAccountId <= 0) {
      wx.showToast({
        title: '请添加小孩信息',
        icon: 'error',
        duration: 2000
      })
      return
    }

    if (this.data.incomeMoney == '' || this.data.incomeMoney == 0) {
      wx.showToast({
        title: '请输入金额',
        icon: 'error',
        duration: 2000
      })
      return
    }

    this.setData({
      showPassword: true
    })

  }

})